﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Configuration;

namespace Extensil.Common.CategoryHandler
{
    public class CategorySettings :ConfigurationElementCollection
    {
        public AddElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as AddElement;
            }
            set
            {
                if(base.Count > index && base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
        public AddElement this[string key]
        {
            get
            {
                return base.BaseGet(key) as AddElement;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }
        public void Add(AddElement ele)
        {
            this.BaseAdd(ele);
        }
        public void Add(string key, string value)
        {
            AddElement ele = new AddElement();
            ele.Key = key;
            ele.Value = value;
            this.BaseAdd(ele);
        }
        internal void Remove(AddElement ele)
        {
            this.BaseRemove(ele.Key);
        }
        internal int IndexOf(AddElement ele)
        {
            return base.BaseIndexOf(ele);
        }
        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return base.CreateNewElement(elementName);
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new AddElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            if(element == null)
                throw new ArgumentNullException("add");

            return ((AddElement)element).Key;
        }
    }
}
